package com.itheima.controller;

import com.itheima.controller.exception.BusinessException;
import com.itheima.controller.exception.ClazzBusinessException;
import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
// 全局异常处理器
@RestControllerAdvice
public class GlobalExceptionGHandler {
    @ExceptionHandler
    public Result error(Exception e){
    log.error("服务器异常{}",e);
    return Result.error("服务器异常");

    }
    @ExceptionHandler
    public Result error(DuplicateKeyException e){
        log.error("重复输入{}",e);
        return Result.error("沙雕输重复了");

    }

    @ExceptionHandler
    public Result error(BusinessException e){
        log.error("业务异常{}",e);
        return Result.error(e.getMessage());

    }
    @ExceptionHandler
    public Result error(ClazzBusinessException e){
        log.error("业务异常{}",e);
        return Result.error(e.getMessage());

    }
}
